package dbobj

import (
	"bytes"
	"fmt"
	"iot-base/common/dbconn"

	"github.com/aidarkhanov/nanoid/v2"
	"gorm.io/datatypes"
	"gorm.io/gorm"
)

//TModel 物模型
type TModel struct {
	UID            string         `gorm:"type:varchar(40);primaryKey;" json:"uid"`
	SName          string         `gorm:"type:varchar(50);not null;uniqueIndex:name_catalog;" json:"s_name"`
	CatalogID      string         `gorm:"type:varchar(40);not null;uniqueIndex:name_catalog" json:"catalog_id"`
	CatalogPtr     *Catalog       `gorm:"foreignKey:CatalogID" json:"-"`
	Desc           string         `json:"desc"`
	Body           datatypes.JSON `json:"body"`
	ContainnerRule datatypes.JSON `json:"containner_rule"`
	IsStation      bool           `json:"is_station"`
	IsEdgeControl  bool           `json:"is_edge_control"`
	HasStream      bool           `json:"has_stream"`
}

func (model *TModel) BeforeCreate(tx *gorm.DB) (err error) {
	if model.ContainnerRule != nil {
		var clen = len(model.ContainnerRule)
		//cr := string(model.ContainnerRule)
		if clen > 6 {

			//bf := bytes.NewBuffer([]byte{})
			//jsonEncoder := json.NewEncoder(bf)
			//jsonEncoder.SetEscapeHTML(false)
			if (model.ContainnerRule[0] == 34 || model.ContainnerRule[0] == 39) &&
				(model.ContainnerRule[clen-1] == 34 || model.ContainnerRule[clen-1] == 39) {
				model.ContainnerRule = bytes.ReplaceAll(model.ContainnerRule[1:clen-1], []byte("\\"), []byte(""))
			}
		}
	}
	return nil

}

func (model *TModel) BeforeSave(tx *gorm.DB) (err error) {
	return model.BeforeCreate(tx)
}

//tx.Exec("delete from thing_shields where target_id=? and target_type = 'thing'",
//t.UID)

func (model *TModel) BeforeDelete(tx *gorm.DB) (err error) {
	tx.Exec("delete from t_model_protocols where model_id=?", model.UID)
	tx.Exec("delete from alarm_shields where target_id=? and target_type = 'model'",
		model.UID)
	return nil
}

//CommonProperty 通用属性
type CommonProperty struct {
	ID    int64          `gorm:"primaryKey;" json:"uid"`
	Body  datatypes.JSON `json:"body"`
	SName string         `json:"s_name"`
}

//TModelProtocol 模型协议
type TModelProtocol struct {
	dbconn.OBJObjIID
	ModelID       string `gorm:"type:varchar(40);not null;uniqueIndex:mp_index_2;" json:"model_id"` //模型id
	Specification string `gorm:"uniqueIndex:mp_index_2;not null;default:''" json:"spec"`
	//ModelPtr      *TModel `gorm:"foreignKey:ModelID;not null" json:"-"`

	ProtocolPath string         `gorm:"uniqueIndex:mp_index_2;" json:"protocol_path"`
	Param        datatypes.JSON `json:"param"`
}

//GetID 获取id
func (pObj *TModel) GetID() interface{} {
	return pObj.UID
}

//AutoID 自动生成id
func (pObj *TModel) AutoID() {
	v, _ := nanoid.New()
	pObj.UID = fmt.Sprintf("%v", v)
}

//Catalog 行业分类
type Catalog struct {
	dbconn.DBObjIDName
	ParentID string         `gorm:"type:varchar(40)" json:"parent_id"` //`gorm:"not null;is:'父节点id'"` //父节点id
	Desc     string         `json:"desc"`
	Rule     datatypes.JSON `json:"rule"` //`gorm:"comment '内容'"` //内容
	//BusinessID string   `gorm:"type:varchar(40)" json:"business_id"` //行业id
	//Business   Business `gorm:"foreignKey:BusinessID"`
}
